//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "UnitedStates.h"
using namespace Cephei::QL::Times::Calendars;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Times::Calendars::CUnitedStates::CUnitedStates (Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::UnitedStates::MarketEnum>^ market) : CCalendar(CUnitedStates::typeid)
{
    try
    {
#ifdef HANDLE
        _phUnitedStates = NULL;
#endif
        QuantLib::UnitedStates::Market _market = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::UnitedStates::MarketEnum>::IsSome::get (market) ? (QuantLib::UnitedStates::Market)market->Value : QuantLib::UnitedStates::Market::Settlement); //10
        _ppUnitedStates = new boost::shared_ptr<QuantLib::UnitedStates> (new QuantLib::UnitedStates ( _market ));
        SetCalendar (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppUnitedStates));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::Calendars::CUnitedStates::CUnitedStates (boost::shared_ptr<QuantLib::UnitedStates>& childNative, Object^ owner) : CCalendar(CUnitedStates::typeid)
{
#ifdef HANDLE
	_phUnitedStates = NULL;
#endif
	_ppUnitedStates = &childNative;
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppUnitedStates));
}
Cephei::QL::Times::Calendars::CUnitedStates::CUnitedStates (QuantLib::UnitedStates& childNative, Object^ owner) : CCalendar(CUnitedStates::typeid)
{
#ifdef HANDLE
	_phUnitedStates = NULL;
#endif
	_ppUnitedStates = new boost::shared_ptr<QuantLib::UnitedStates> (&childNative);
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppUnitedStates));
    _UnitedStatesOwner = owner;
    _CalendarOwner = owner;
}

Cephei::QL::Times::Calendars::CUnitedStates::CUnitedStates (CUnitedStates^ copy) : CCalendar(CUnitedStates::typeid)
{
#ifdef HANDLE
	_phUnitedStates = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppUnitedStates = new boost::shared_ptr<QuantLib::UnitedStates> (copy->GetShared());
        _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppUnitedStates));
    }
}
Cephei::QL::Times::Calendars::CUnitedStates::CUnitedStates (PLATFORM::Type^ t) : CCalendar(CUnitedStates::typeid)
{
#ifdef HANDLE
	_phUnitedStates = NULL;
#endif
	if (!t->IsSubclassOf(CUnitedStates::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::Calendars::CUnitedStates::CUnitedStates (QuantLib::Handle<QuantLib::UnitedStates>& childNative, Object^ owner)  : CCalendar(CUnitedStates::typeid)
{
	_phUnitedStates = &childNative;
	_ppUnitedStates = &static_cast<boost::shared_ptr<QuantLib::UnitedStates>>(childNative.currentLink());
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppUnitedStates));
    _UnitedStatesOwner = owner;
}
Cephei::QL::Times::Calendars::CUnitedStates::CUnitedStates (QuantLib::Handle<QuantLib::UnitedStates> childNative)  : CCalendar(CUnitedStates::typeid)
{
	_phUnitedStates = &childNative;
	_ppUnitedStates = &static_cast<boost::shared_ptr<QuantLib::UnitedStates>>(childNative.currentLink());
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppUnitedStates));
}
#endif
#ifdef STRUCT
Cephei::QL::Times::Calendars::CUnitedStates::CUnitedStates (QuantLib::UnitedStates childNative)  : CCalendar(CUnitedStates::typeid)
{
#ifdef HANDLE
	_phUnitedStates = NULL;
#endif
	_ppUnitedStates = new boost::shared_ptr<QuantLib::UnitedStates> (new QuantLib::UnitedStates (childNative));
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppUnitedStates));
}
#endif

Cephei::QL::Times::Calendars::CUnitedStates::~CUnitedStates ()
{
    if (_ppUnitedStates != NULL)
    {
	    delete _ppUnitedStates;
        _ppUnitedStates = NULL;
    }
}
Cephei::QL::Times::Calendars::CUnitedStates::!CUnitedStates ()
{
    if (_ppUnitedStates != NULL)
    {
	    delete _ppUnitedStates;
    }
}
QuantLib::UnitedStates& Cephei::QL::Times::Calendars::CUnitedStates::GetReference ()
{
    if (_ppUnitedStates == NULL) throw REFNEW NativeNullException ();
	return **_ppUnitedStates;
}
boost::shared_ptr<QuantLib::UnitedStates>& Cephei::QL::Times::Calendars::CUnitedStates::GetShared ()
{
    if (_ppUnitedStates == NULL) throw REFNEW NativeNullException ();
	return *_ppUnitedStates;
}
QuantLib::UnitedStates* Cephei::QL::Times::Calendars::CUnitedStates::GetPointer ()
{
    if (_ppUnitedStates == NULL) throw REFNEW NativeNullException ();
	return &**_ppUnitedStates;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::UnitedStates>& Cephei::QL::Times::Calendars::CUnitedStates::GetHandle ()
{
	if (_phUnitedStates == NULL)
	{
		_phUnitedStates = new Handle<QuantLib::UnitedStates> (*_ppUnitedStates);
	}
	return *_phUnitedStates;
}
#endif
bool Cephei::QL::Times::Calendars::CUnitedStates::HasNative () 
{
	return (_ppUnitedStates != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::Calendars::IUnitedStates^ Cephei::QL::Times::Calendars::CUnitedStates_Factory::Create (Microsoft::FSharp::Core::FSharpOption<QL::Times::Calendars::UnitedStates::MarketEnum>^ market)
{
    return REFNEW CUnitedStates ( market);
}
